import axios, { type AxiosRequestConfig  } from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import '@/style/nprogress.less'

const request = axios.create({
  baseURL: import.meta.env.VITE_AXIOS_BASE_URL,
  timeout: import.meta.env.VITE_AXIOS_TIMEOUT
})

request.interceptors.request.use(
  config => {
    NProgress.start()
    return config
  }, err => {
    return Promise.reject(err)
  }
)

request.interceptors.response.use(
  res => {
    NProgress.done()
    return res.data
  }, err => {
    return Promise.reject(err)
  }
)

export default ({ url, method, data, params, headers }:AxiosRequestConfig)  => {
  return request({
    url,
    method,
    data,
    params,
    headers
  })
}
